import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author zhaoren
 * @version 1.0
 * @description context上下文,用于配置 {@link Strategy}
 * @date 2020/7/30 14:12
 **/
public class Context {


    private static final String ALGORITHM;
    private static final String BASE_NAME = "ConcreateStrategy";

    /**
     * 引用strategy对象，用于维护具体的strategy
     */
    private Strategy strategy;

    static {
        InputStream is = ClassLoader.getSystemResourceAsStream("Algorithm.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ALGORITHM = properties.getProperty("algorithm");
    }

    /**
     * 初始化context对象的时候就要传入具体标记来创建具体的strategy对象
     */
    public Context() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> strategyClass = Class.forName(BASE_NAME + ALGORITHM);
        strategy = ((Strategy) strategyClass.newInstance());
    }

    /**
     * 上下文接口，根据具体的策略对象调整其算法
     */
    public void contextInterface(){
        strategy.algorithmInterface();
    }


}
